package coderbyte;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * My free self imposed challenges
 *
 * @author ascorbin
 */
public class MyOwn {

    /**
     * specifies the least number of coins, that when added, equal the input
     * parameter
     *
     * @param input
     * @return least number of coins to build the parameters
     */
    int CoinDeterminer(int input) {
        int[] coinsUsed = new int[input + 1];
        int[] lastCoin = new int[input + 1];
        int[] cnArr = {11, 9, 7, 5, 1};
        coinsUsed[0] = 0;
        lastCoin[0] = 1;

        for (int cents = 1; cents <= input; cents++) {
            int minCoins = cents;
            int newCoin = 1;

            for (int j = 0; j < cnArr.length; j++) {
                if (cnArr[j] > cents) // Cannot use coin j
                {
                    continue;
                }
                if (coinsUsed[cents - cnArr[j]] + 1 < minCoins) {
                    minCoins = coinsUsed[cents - cnArr[j]] + 1;
                    newCoin = cnArr[j];
                }
            }

            coinsUsed[cents] = minCoins;
            lastCoin[cents] = newCoin;
        }
//        System.out.println(Arrays.toString(coinsUsed));
//        System.out.println(Arrays.toString(lastCoin));
//        System.out.println("Best is " + coinsUsed[maxChange] + " coins");
//        for (int i = maxChange; i > 0;) {
//            System.out.print(lastCoin[i] + " ");
//            i -= lastCoin[i];
//        }
        return coinsUsed[input];
    }

    /**
     * My first recursive method taken from "Java ist auch eine Insel"
     *
     * @param arg
     */
    void down2(int arg) {
        if (arg <= 0) {
            return;
        }
        down2(arg - 1);
        System.out.print(arg + ",");

    }

    /**
     * Searches the number of distinct values in an array of integer
     *
     * @param arg Array of Integer
     * @return Integer
     */
    Integer numDisInt(Integer[] arg) {
        int res = 0;
        for (int i = 0; i < arg.length; i++) {
            boolean seenThisIntBefore = false;
            for (int j = 0; j < i; j++) {
                if (Objects.equals(arg[i], arg[j])) {
                    seenThisIntBefore = true;
                }
            }
            if (!seenThisIntBefore) {
                res++;
            }
        }
        return res;
    }

    /**
     * Calculates the deutsche-mark currency value of parameter
     *
     * @param arg String
     * @return String
     */
    String dmInEuro(String arg) {
        DecimalFormat df = new DecimalFormat("0.00");
        double euroKurs = 1.95583;
        double d = Double.parseDouble(arg);
        double div = (double) d / euroKurs;
//        div = Math.round(div*100.00)/100.00;
        String res = df.format(div);
        return res;
    }

    /**
     * Calculates the euro currency value of parameter
     *
     * @param arg String
     * @return String
     */
    String euroInDm(String arg) {
        DecimalFormat df = new DecimalFormat("0.00");
        double euroKurs = 1.95583;
        double d = Double.parseDouble(arg);
        double div = (double) d * euroKurs;
//        div = Math.round(div*100.00)/100.00;
        String res = df.format(div);//Double.toString(div);
        return res;
    }

    /**
     * Returns a pseudo-random number between min and max, inclusive. The
     * difference between min and max can be at most
     * <code>Integer.MAX_VALUE - 1</code>.
     *
     * @param min Minimum value
     * @param max Maximum value. Must be greater than min.
     * @return Integer between min and max, inclusive.
     * @see java.util.Random#nextInt(int)
     */
    Integer randInt(int min, int max) {

        // NOTE: Usually this should be a field rather than a method
        // variable so that it is not re-seeded every call.
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

    void randArray() {
        List<Integer> rndArr = new ArrayList<>();

        for (int i = 1; i < 25; i++) {
            rndArr.add(randInt(0, 100));
        }

        System.out.println(rndArr);
    }

    /**
     * Returns a formatted date from parameter
     *
     * @param arg string
     * @return date
     */
    Date stringToDate(String arg) {
        SimpleDateFormat format = new SimpleDateFormat("h:mma");
        Date date = null;

        try {
            date = format.parse(arg);
        } catch (ParseException pe) {
            System.out.println(pe);
        }
        return date;
    }
}
